
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';


///  指定三角大小，指定三角偏移量的，带背景颜色


class TriangleWidget extends StatelessWidget {
  final Color color;//三角形颜色
  final double offsetX; // 左右偏移量
  final double triangleW;
  final double triangleH;
  const TriangleWidget({this.color = Colors.white,this.offsetX = 40,this.triangleW = 12,this.triangleH = 10,super.key});

  @override
  Widget build(BuildContext context) {
    return CustomPaint(
      size: Size(triangleW, triangleH), // 指定绘制区域的大小
      painter: TrianglePainter(offsetX: offsetX),
    );
  }
}

class TrianglePainter extends CustomPainter {
  final double offsetX; // 左右偏移量
  final Color color;//三角形颜色
  TrianglePainter({this.offsetX = 0.0,this.color = Colors.white});

  @override
  void paint(Canvas canvas, Size size) {
    final paint = Paint()
      ..color = color
      ..style = PaintingStyle.fill;

    final path = Path();
    path.moveTo(size.width / 2 + offsetX, 0); // 顶点，加上偏移
    path.lineTo(size.width + offsetX, size.height); // 右下角，加上偏移
    path.lineTo(0 + offsetX, size.height); // 左下角，加上偏移
    path.close();

    canvas.drawPath(path, paint);
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    return false;
  }
}
